#include "stdafx.h"
#include "ReceiveManager.h"


CReceiveManager::CReceiveManager()
{
}


CReceiveManager::~CReceiveManager()
{
	for (ReceiverListPos pos2 = m_PendingList.begin(); pos2 != m_PendingList.end(); ++pos2)
	{
		CReceiver* curData = *pos2;
		delete curData;
		curData = NULL;
	}
	m_PendingList.clear();

	for (ReceiverListPos pos2 = m_DeletedList.begin(); pos2 != m_DeletedList.end(); ++pos2)
	{
		CReceiver* curData = *pos2;
		delete curData;
		curData = NULL;
	}
	m_DeletedList.clear();
}

CReceiver* CReceiveManager::AddReceiver(CReceiver* a_Rcvr)
{
	if (a_Rcvr)
	{
		m_PendingList.push_back(a_Rcvr);
	}

	return a_Rcvr;
}

void CReceiveManager::Execute()
{
	if (m_PendingList.size() > 0)
	{
		CReceiver* rcvr = m_PendingList.front();
		rcvr->Execute();
	}
}

CReceiver* CReceiveManager::Pop()
{
	CReceiver* rcvr = NULL;
	if (m_PendingList.size() > 0)
	{
		rcvr = m_PendingList.front();
		m_PendingList.pop_front();
	}

	return rcvr;
}

CReceiver* CReceiveManager::OnComplete()
{
	CReceiver* pPrev = Pop();
	Execute();
	if (pPrev)
	{
		m_DeletedList.push_back(pPrev);
	}

	return pPrev;
}
